package com.example.blog.service.impl;

import com.example.blog.pojo.type;
import com.example.blog.dao.typeRepository;
import com.example.blog.service.typeService;
import com.github.pagehelper.Page;
import org.apache.ibatis.javassist.NotFoundException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class typeServiceImpl implements typeService {

    @Autowired
    typeRepository typeRepository;

    @Transactional
    @Override
    public void saveType(type type) {
        typeRepository.save(type);
    }

    @Override
    public type getType(Integer id) {
        return typeRepository.findById(id);
    }

    @Override
    public type getType(String name) {
        return typeRepository.findByName(name);
    }

    @Override
    public Page<type> listType() {
        return typeRepository.findAll();
    }

    @Override
    public Page<type> list() {
        return typeRepository.find();
    }

    @Transactional
    @Override
    public void updateType(Integer id, String name) throws NotFoundException {
        type t = typeRepository.findById(id);
        if (t == null){
            throw new NotFoundException("不存在该类型");
        }else {
            typeRepository.update(id,name);
        }

    }

    @Transactional
    @Override
    public void deleteType(Integer id) {
        typeRepository.delete(id);
    }

    @Override
    public Integer getCount() {
        return typeRepository.count();
    }
}
